package net.tngou.iask.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import net.tngou.iask.dao.AccessDao;
import net.tngou.iask.dao.AnalysisDao;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.jdbc.QueryHelper;
import net.tngou.iask.pojo.Access;
import net.tngou.iask.pojo.Accessresult;
import net.tngou.iask.pojo.Health;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.util.StringUtil;

/**
 * 访问
 * @author tngou@tngou.net
 *
 */
public class AccessService extends BaseService {

	/**
	 * 取得访问列表
	 * @param page
	 * @param size
	 * @param main
	 * @param type
	 * @param keyword
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,int type,String keyword) {
		AccessDao dao = new AccessDao();
		return dao.getPage(page, size, main, type, keyword);
	}
	public PageUtil getPage(int page,int size,long main,long disease) {
		AccessDao dao = new AccessDao();
		return dao.getPage(page, size, main, disease);
	}
	
	public PageUtil getPage(int page,int size,long main,int type,int state, String keyword) {
		AccessDao dao = new AccessDao();
		return dao.getPage(page, size, main, type,state, keyword);
	}
	/**
	 * 保存结果
	 * @param pojo
	 * @param result
	 * @return
	 */
	public long save(POJO pojo ,Accessresult result)
	{
		long id=0;
		POJO bean = new Accessresult().get("access="+result.getAccess()+"question="+result.getQuestion());
		if(bean==null)
		{
			id=result.save();
		}else
		{
			result.setId(bean.getId());
			id=result.update();
		}
		try {
			Health health=new Health();
			BeanUtils.copyProperties(health, result);
			if(pojo==null)
			{
				health.setId(0);
				health.save();
			}else
			{
				health.setId(pojo.getId());
				health.update();
			}
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id;
		
	}
	
	/**
	 * 取得患者访问
	 * @param main
	 * @param patient
	 * @param type
	 * @return
	 */
	public List<Map<String, Object>> getList(long main,long patient,int type){
		AccessDao dao = new AccessDao();
		return dao.getList(main, patient, type);
	}
	/**
	 * 取得治疗随访
	 * @param cure
	 * @return
	 */
	public PageUtil getList(int page,int size,long cure) {
		AccessDao dao = new AccessDao();
		return dao.getList(page, size, cure);
	}
	
	/**
	 * 取得信息
	 * @param patient
	 * @return
	 */
	public List<Accessresult> getAccessresult(long patient,List<Access> list)
	{
		List<Long> ids = new ArrayList<>();
		list.forEach(e->{ids.add(e.getId());});
		if(ids.isEmpty()) return new ArrayList<>();
		AccessDao dao = new AccessDao();
		return dao.getAccessresult(patient, ids.toArray());
	}
	
	
	public int access(long main,long question,List<Map<String, Object>> list) {
		List<Long> ids = new ArrayList<>();
		list.forEach(e->{ids.add(Long.parseLong(e.get("id").toString()));});
		if(ids.isEmpty()) return 0;
		AccessDao dao = new AccessDao();
		return dao.access(main, question, ids.toArray());
	}
	
	public int accessMax(long main,long question,List<Map<String, Object>> list) {
		List<Long> ids = new ArrayList<>();
		list.forEach(e->{ids.add(Long.parseLong(e.get("id").toString()));});
		if(ids.isEmpty()) return 0;
		AccessDao dao = new AccessDao();
		return dao.accessMax(main, question, ids.toArray());
	}
	public int accessMin(long main,long question,List<Map<String, Object>> list) {
		List<Long> ids = new ArrayList<>();
		list.forEach(e->{ids.add(Long.parseLong(e.get("id").toString()));});
		if(ids.isEmpty()) return 0;
		AccessDao dao = new AccessDao();
		return dao.accessMin(main, question, ids.toArray());
	}
	
	public int accessresult(long main,long question,List<Map<String, Object>> list) {
		List<Long> ids = new ArrayList<>();
		list.forEach(e->{ids.add(Long.parseLong(e.get("id").toString()));});
		if(ids.isEmpty()) return 0;
		AccessDao dao = new AccessDao();
		return dao.accessresult(main, question, ids.toArray());
	}
	public int accessresultvalue(long main,long question,List<Map<String, Object>> list) {
		List<Long> ids = new ArrayList<>();
		list.forEach(e->{ids.add(Long.parseLong(e.get("id").toString()));});
		if(ids.isEmpty()) return 0;
		AccessDao dao = new AccessDao();
		return dao.accessresultvalue(main, question, ids.toArray());
	}
	
}
